import { createSlice } from "@reduxjs/toolkit";
import store from "store2";
const adminSlice = createSlice({
  name: "admin",
  initialState: {
    LoginState: store.get("users") ? store.get("users")["LoginState"] : false, // 登录的状态
    adminname: store.get("users") ? store.get("users")["adminname"] : "", // 用户名
    token: store.get("users") ? store.get("users")["token"] : "", // token令牌
    role: store.get("users") ? store.get("users")["role"] : 0, // 用户的角色
    checkedKeys: store.get("users") ? store.get("users")["checkedKeys"] : [], // 用户的权限数组
    open: store.get("open") || false,
    editAdmin: store.get("editAdmin") || {
      adminname: "",
      password: "",
      role: "2",
      checkedKeys: [],
    },
  },
  reducers: {
    changeLoginState(state, action) {
      state.LoginState = action.payload;
    },
    changeAdminName(state, action) {
      state.adminname = action.payload;
    },
    changeToken(state, action) {
      state.token = action.payload;
    },
    changeRole(state, action) {
      state.role = action.payload;
    },
    changeCheckedKeys(state, action) {
      state.checkedKeys = action.payload;
    },
    changeOpen(state, action) {
      state.open = action.payload;
    },
    changeEditAdmin(state, action) {
      state.editAdmin = action.payload;
    },
  },
});
export const {
  changeLoginState,
  changeAdminName,
  changeToken,
  changeRole,
  changeCheckedKeys,
  changeOpen,
  changeEditAdmin,
} = adminSlice.actions;
export default adminSlice.reducer;
